<template>
  <van-list v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            :error.sync="error"
            error-text="请求失败，点击重新加载"
            @load="onLoad">
    <van-cell v-for="item in list"
              :key="item.art_id"
              :title="item.title" />
  </van-list>
</template>

<script>
import { getSearchResults } from '@/api/search.js'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      error: false
    }
  },
  props: {
    searchtext: {
      type: String,
      required: true
    }
  },
  created () {

  },

  methods: {
    async onLoad () {
      try {
        const { data: res } = await getSearchResults({
          page: this.page,
          per_page: 10,
          q: this.searchtext
        })
        console.log(res)
        if (Math.random() > 0.5) {
          console.lg(123)
        }
        this.list.push(...res.data.results)
        this.loading = false
        if (res.data.results.length) {
          this.page++
        } else {
          this.finished = true
        }
      } catch (err) {
        this.error = true
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
